import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:gw_github_flutter/common/global.dart';
import 'package:gw_github_flutter/common/route_name.dart';
import 'package:gw_github_flutter/models/tbSectionModel.dart';
import 'package:sticky_and_expandable_list/sticky_and_expandable_list.dart';

class SliverRouteTableViewCustomSection extends StatefulWidget {
  SliverRouteTableViewCustomSection({Key? key}) : super(key: key);

  @override
  State<SliverRouteTableViewCustomSection> createState() =>
      _SliverRouteTableViewCustomSectionState();
}

class _SliverRouteTableViewCustomSectionState
    extends State<SliverRouteTableViewCustomSection> {
  var list = Global.getTBSectionsList();

  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(
            appBar: AppBar(
              title: Text(
                RouteName.sliverRoute_tableView,
                style: TextStyle(fontSize: 14),
              ),
            ),
            body: EasyRefresh.builder(
              onRefresh: () {},
              childBuilder: (context, physics) {
                return CustomScrollView(
                  physics: physics,
                  slivers: [
                    const HeaderLocator.sliver(),
                    SliverExpandableList(
                      builder: SliverExpandableChildDelegate<dynamic,
                              TbSectionModel>(
                          sectionList: list,
                          sectionBuilder: (context, containerInfo) {
                            containerInfo
                              ..header = buildHeader(context, containerInfo)
                              ..content = _buildContent(context, containerInfo);
                            return ExpandableSectionContainer(
                              info: containerInfo,
                            );
                          },
                          itemBuilder:
                              (context, sectionIndex, itemIndex, index) {
                            String item = list[sectionIndex].items?[itemIndex];
                            if (sectionIndex % 2 == 0) {
                              return _cellOne(item, index);
                            }
                            return _cellTwo(item, index);
                          }),
                    )
                  ],
                );
              },
            )));
  }

  Widget _cellOne(String item, int index) {
    return Container(
      child: Column(
        // mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Expanded(
              flex: 1,
              child: Image.asset(
                "imgs/fengjing.png",
                fit: BoxFit.fill,
              )),
          Padding(
              padding: EdgeInsets.all(10),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [Text(item), Text("$index")],
              ))
        ],
      ),
    );
  }

  Widget _cellTwo(String item, int index) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Container(
          padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 0),
          child: Row(
            children: [
              CircleAvatar(
                child: Text("$index"),
              ),
              Text(item),
            ],
          ),
        ),
      ],
    );
  }

  Widget buildHeader(
      BuildContext context, ExpandableSectionContainerInfo containerInfo) {
    var sectionIndex = containerInfo.sectionIndex;
    TbSectionModel item = list[sectionIndex];
    return GestureDetector(
      child: Container(
          color: Colors.lightBlue,
          height: 100,
          padding: const EdgeInsets.only(left: 20),
          alignment: Alignment.centerLeft,
          child: Text(
            item.header ?? "",
            style: const TextStyle(color: Colors.white),
          )),
    );
  }

  Widget _buildContent(
      BuildContext context, ExpandableSectionContainerInfo containerInfo) {
    TbSectionModel section = list[containerInfo.sectionIndex];
    if (!section.isSectionExpanded()) {
      return const SizedBox.shrink();
    }
    if (containerInfo.sectionIndex % 2 == 0) {
      return GridView.builder(
          padding: EdgeInsets.all(10),
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2, mainAxisSpacing: 10, crossAxisSpacing: 10),
          shrinkWrap: true,
          physics: const NeverScrollableScrollPhysics(),
          itemBuilder: containerInfo.childDelegate!.builder as Widget Function(
              BuildContext, int),
          itemCount: containerInfo.childDelegate!.childCount,
      );
    }
    return ListView.builder(
      itemExtent: 50,
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      itemBuilder: containerInfo.childDelegate!.builder as Widget Function(
          BuildContext, int),
      itemCount: containerInfo.childDelegate!.childCount,
    );
  }
}
